package com.ruoyi.web.controller.fund;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.fileDetail.domain.HouseFileDetail;
import com.ruoyi.fileDetail.service.IHouseFileDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 档案列表Controller
 *
 * @author lijie
 * @date 2025-03-21
 */
@RestController
@RequestMapping("/fund/fileDetail")
public class HouseFileDetailController extends BaseController {
    @Autowired
    private IHouseFileDetailService houseFileDetailService;

    /**
     * 查询档案列表列表
     */
    @GetMapping("/list")
    public TableDataInfo list(HouseFileDetail houseFileDetail) {
        startPage();
        List<HouseFileDetail> list = houseFileDetailService.selectHouseFileDetailList(houseFileDetail);
        for(HouseFileDetail houseFileDetailVO : list){
            houseFileDetailVO.setFileDetailUrl(RuoYiConfig.getFileCabinetHttp()+houseFileDetailVO.getFileDetailUrl());
        }
        return getDataTable(list);
    }

    @PostMapping("/upload")
    public AjaxResult uploadFile(MultipartFile file, String fileDetailName, String fileDetailDescription, Long filingId) throws Exception {
        try {
            HouseFileDetail houseFileDetail = new HouseFileDetail();
            String filePath = RuoYiConfig.getFileCabinet();
            // 上传并返回新文件名称
            String fileName = FileUploadUtils.upload(filePath, file).substring(1);
            houseFileDetail.setFilingId(filingId);
            houseFileDetail.setFileDetailName(fileDetailName);
            houseFileDetail.setFileDetailDescription(fileDetailDescription);
            houseFileDetail.setFileDetailUrl(fileName);
            houseFileDetail.setCreater(getUsername());
            houseFileDetailService.insertHouseFileDetail(houseFileDetail);
            AjaxResult ajax = AjaxResult.success();
            return ajax;
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 获取档案列表详细信息
     */
    @GetMapping(value = "/{fileDetailId}")
    public AjaxResult getInfo(@PathVariable("fileDetailId") Long fileDetailId) {
        return AjaxResult.success(houseFileDetailService.selectHouseFileDetailByFileDetailId(fileDetailId));
    }

    /**
     * 新增档案列表
     */
    @Log(title = "档案列表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HouseFileDetail houseFileDetail) {
        return toAjax(houseFileDetailService.insertHouseFileDetail(houseFileDetail));
    }

    /**
     * 修改档案列表
     */
    @Log(title = "档案列表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HouseFileDetail houseFileDetail) {
        return toAjax(houseFileDetailService.updateHouseFileDetail(houseFileDetail));
    }

    /**
     * 删除档案列表
     */
    @Log(title = "档案列表", businessType = BusinessType.DELETE)
    @DeleteMapping("/{fileDetailId}")
    public AjaxResult remove(@PathVariable Long fileDetailId) {
        HouseFileDetail houseFileDetail = houseFileDetailService.selectHouseFileDetailByFileDetailId(fileDetailId);
        houseFileDetailService.deleteHouseFileDetailByFileDetailId(fileDetailId);
        FileUtils.deleteFile(RuoYiConfig.getFileCabinet()+houseFileDetail.getFileDetailUrl());
        return toAjax(1);
    }
}
